package com.woniuxy.shiro.service.impl;

import com.woniuxy.entity.RbacManager;
import com.woniuxy.shiro.dao.LoginDao;
import com.woniuxy.shiro.service.LoginService;
import com.woniuxy.utils.JWTUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired(required = false)
    LoginDao loginDao;
    @Override
    public RbacManager login(String username, String password) {
//        new Md5Hash() 可以省略“MD5”
        SimpleHash simpleHash = new SimpleHash("MD5", password, username + "woniuxy");
        String newPassword = simpleHash.toHex();
        RbacManager rbacManager = loginDao.login(username, newPassword);
        //在这个RbacManager对象中封装token
        String jwt = JWTUtils.sign(username, newPassword);
        rbacManager.setToken(jwt);
        return rbacManager;
    }


    @Override
    public RbacManager loginAuth(String username, String password) {
        RbacManager rbacManager = loginDao.login(username, password);
        return rbacManager;
    }
}
